package com.test28_类库应用;

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

/**
 * 定义核心的业务接口
 */
interface INumber {
    /** 获取对应的数据 */
    public int[] getData();
}

/**
 * 自定义异常
 */
class InvalidateArrayLengthException extends RuntimeException {
    public InvalidateArrayLengthException() {
    }

    public InvalidateArrayLengthException(String str) {
        super(str);
    }
}

/**
 * 实现随机数字
 */
class RandomNumber implements INumber {
    private int[] data;

    public RandomNumber() {
        this(5);
    }

    public RandomNumber(int len) {
        if (len <= 0) {
            throw new InvalidateArrayLengthException("当前所设置的数组长度不正确。");
        }
        this.data = new int[len];
        this.createRandom();
    }

    /** 实现数据的创建 */
    private void createRandom() {
        Random random = new Random();
        for (int x = 0; x < this.data.length; x++) {
            // 生成1~30之间的随机数
            this.data[x] = random.nextInt(30) + 1;
        }
    }

    @Override
    public int[] getData() {
        return this.data;
    }
}

class NumberFactory {
    private NumberFactory() {
    }

    public static INumber getInstance(int... args) {
        if (args.length == 0) {
            return new RandomNumber();
        } else {
            return new RandomNumber(args[0]);
        }
    }
}

/**
 * 生成随机数
 */
public class YootkDemo_02 {

    @Test
    public void test1() {
        Random random = new Random();
        for (int x = 0; x < 100; x++) {
            int num = random.nextInt(30) + 1;
            System.out.println(num);
        }
    }

    @Test
    public void test2() {
        INumber number = NumberFactory.getInstance();
        System.out.println(Arrays.toString(number.getData()));
    }
}
